namespace FastDevTemplate.Common.Util;

public enum ErrorType
{
    NoError = 0,
    BaseError = -1,

    //参数错误类码：
    ArgumentVerifyError = -11,
    ArgumentFormatError = -12,

    //逻辑错误类码：
    LogicError = -20,
    LogicNotFindEntityError = -21,
    LogicLoginError = -22,
    LogicTokenError = -23,
    LogicPurviewError = -24,
    DataBaseSaveError = -25,
    DataBaseConnectionError = -26,
    DataBaseDesignError = -27,
    GroupError = -28,

    //连接错误类码
    ConnectionHandleTimeoutError = -31
}

public static class ExceptionFactory
{
    public static BaseException GetException(ErrorType errorType)
    {
        switch (errorType)
        {
            case ErrorType.ArgumentVerifyError:
                return new ArgumentVerifyErrorException();
            case ErrorType.ArgumentFormatError:
                return new ArgumentFormatErrorException();
            case ErrorType.LogicError:
                return new LogicErrorException();
            case ErrorType.LogicNotFindEntityError:
                return new LogicNotFindEntityErrorException();
            case ErrorType.LogicLoginError:
                return new LogicLoginErrorException();
            case ErrorType.LogicPurviewError:
                return new LogicPurviewErrorException();
            case ErrorType.DataBaseSaveError:
                return new DataBaseSaveErrorException();
            case ErrorType.DataBaseConnectionError:
                return new DataBaseConnectionErrorException();
            case ErrorType.DataBaseDesignError:
                return new DataBaseDesignErrorException();
            case ErrorType.ConnectionHandleTimeoutError:
                return new ConnectionHandleTimeoutErrorException();
            case ErrorType.GroupError:
                return new GroupErrorErrorException();
            default:
                return new BaseException();
        }
    }
    public static string GetErrorMessage(int errorType) 
    {
        return GetErrorMessage(GetErrorType(errorType));
    }

    public static string GetErrorMessage(ErrorType errorType)
    {
        switch (errorType)
        {
            case ErrorType.ArgumentVerifyError:
                return $"参数校验错误";
            case ErrorType.ArgumentFormatError:
                return $"参数格式错误";
            case ErrorType.LogicError:
                return $"业务逻辑错误";
            case ErrorType.LogicNotFindEntityError:
                return $"操作目标未找到";
            case ErrorType.LogicLoginError:
                return $"登录验证错误";
            case ErrorType.LogicPurviewError:
                return $"用户权限错误";
            case ErrorType.DataBaseSaveError:
                return $"数据保存错误";
            case ErrorType.DataBaseConnectionError:
                return $"数据库连接错误";
            case ErrorType.DataBaseDesignError:
                return $"数据库设计错误";
            case ErrorType.ConnectionHandleTimeoutError:
                return $"连接超时错误";
            case ErrorType.GroupError:
                return $"操作目标未分组或分组不同";
            default:
                return $"基础错误";
        }
    }

    public static int Error(ErrorType errorType)
    {
        return (int)errorType;
    }

    public static ErrorType GetErrorType(int errorCode)
    {
        if (errorCode >= 0) return ErrorType.NoError;
        return (ErrorType)errorCode;
    }
}

public class BaseException : Exception
{
    public readonly ErrorType ErrorCode;

    public BaseException() : base(ExceptionFactory.GetErrorMessage(ErrorType.BaseError))
    {
        ErrorCode = ErrorType.BaseError;
    }

    protected BaseException(ErrorType errorType, string message) : base(message)
    {
        ErrorCode = errorType;
    }
}

public class ArgumentVerifyErrorException : BaseException
{
    public ArgumentVerifyErrorException() :
        base(ErrorType.ArgumentVerifyError, ExceptionFactory.GetErrorMessage(ErrorType.ArgumentVerifyError))
    {
    }
}

public class ArgumentFormatErrorException : BaseException
{
    public ArgumentFormatErrorException() :
        base(ErrorType.ArgumentFormatError, ExceptionFactory.GetErrorMessage(ErrorType.ArgumentFormatError))
    {
    }
}

public class LogicErrorException : BaseException
{
    public LogicErrorException() :
        base(ErrorType.LogicError, ExceptionFactory.GetErrorMessage(ErrorType.LogicNotFindEntityError))
    {
    }
}


public class LogicNotFindEntityErrorException : BaseException
{
    public LogicNotFindEntityErrorException() :
        base(ErrorType.LogicNotFindEntityError, ExceptionFactory.GetErrorMessage(ErrorType.LogicNotFindEntityError))
    {
    }
}

public class LogicLoginErrorException : BaseException
{
    public LogicLoginErrorException() :
        base(ErrorType.LogicLoginError, ExceptionFactory.GetErrorMessage(ErrorType.LogicLoginError))
    {
    }
}

public class LogicPurviewErrorException : BaseException
{
    public LogicPurviewErrorException() :
        base(ErrorType.LogicPurviewError, ExceptionFactory.GetErrorMessage(ErrorType.LogicPurviewError))
    {
    }
}

public class DataBaseSaveErrorException : BaseException
{
    public DataBaseSaveErrorException() :
        base(ErrorType.DataBaseSaveError, ExceptionFactory.GetErrorMessage(ErrorType.DataBaseSaveError))
    {
    }
}

public class DataBaseConnectionErrorException : BaseException
{
    public DataBaseConnectionErrorException() :
        base(ErrorType.DataBaseConnectionError, ExceptionFactory.GetErrorMessage(ErrorType.DataBaseConnectionError))
    {
    }
}

public class DataBaseDesignErrorException : BaseException
{
    public DataBaseDesignErrorException() :
        base(ErrorType.DataBaseConnectionError, ExceptionFactory.GetErrorMessage(ErrorType.DataBaseDesignError))
    {
    }
}

public class ConnectionHandleTimeoutErrorException : BaseException
{
    public ConnectionHandleTimeoutErrorException() :
        base(ErrorType.ConnectionHandleTimeoutError,
            ExceptionFactory.GetErrorMessage(ErrorType.ConnectionHandleTimeoutError))
    {
    }
}

public class GroupErrorErrorException : BaseException
{
    public GroupErrorErrorException() :
        base(ErrorType.ConnectionHandleTimeoutError,
            ExceptionFactory.GetErrorMessage(ErrorType.GroupError))
    {
    }
}